package com.web.system.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.system.po.RolePo;
import com.web.system.service.RoleService;

@Service("roleService")
@Transactional(rollbackFor = { Exception.class })
public class RoleDAO implements RoleService {

	@Resource 
	private SessionFactory sessionFactory;
	/**
	 * 分页获取role
	 */
	@SuppressWarnings("unchecked")
	public List<RolePo> getRoleList(String queryString ,int first,int pageSize) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		 session = this.sessionFactory.getCurrentSession();
      		 query = session.createQuery(queryString);
             query.setFirstResult(first);
             query.setMaxResults(pageSize);
             List<RolePo> roles = query.list(); 
             return roles;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
		} 
		return null;
	}

	/**
	 * 获得roleList数据条数
	 */

	public int getTotal(String sql) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		session = this.sessionFactory.getCurrentSession();
      		String queryString = "select count(*) from RolePo as role where 1=1 " + sql;
      		query = session.createQuery(queryString);       
      		int count = ((Long)query.uniqueResult()).intValue();
      		return count;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 保存role同时保存其所对应的树形菜单
	 */

	public boolean saveOrUpdateRoleAndMenu(RolePo role) {
		// TODO Auto-generated method stub
		Session session = null;
		try {
			session = this.sessionFactory.getCurrentSession();
			session.saveOrUpdate(role);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	/**
	 * 
	 */

	public RolePo getRoleByRoleId(Integer roleId) {
		// TODO Auto-generated method stub
		Session session = null;
		Query query = null;
		try {
			session = this.sessionFactory.getCurrentSession();
			StringBuffer hql = new StringBuffer();
			hql.append("Select a From RolePo a");
			hql.append(" left outer join fetch a.treeMenuPos");
			//hql.append(" left outer join fetch a.userBeans");
			hql.append(" where a.roleId = '");
			hql.append(roleId);
			hql.append("'");
			query = session.createQuery(hql.toString());
			RolePo rolePo = (RolePo) query.list().get(0);
			return rolePo;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		return null;
	}

	/**
	 * 根据hql语句来执行更新操作，返回记录数
	 */

	public int executeUpdates(String sql) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		 session = this.sessionFactory.getCurrentSession();
      		 query = session.createQuery(sql);
      		 int count = query.executeUpdate();
             return count;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
      		throw new RuntimeException();
		} 
	}

	/**
	 * 角色名称是否被占用
	 */

	public boolean isExistRole(String roleName) {
		// TODO Auto-generated method stub
		if(roleName==null || "".equals(roleName) || "null".equalsIgnoreCase(roleName)){
			return false;
		}
		Session session = null;
		Query query = null;
		String sql = "select count(*) from RolePo where roleName = '"+roleName+"'";
		try {
			session = this.sessionFactory.getCurrentSession();
			query = session.createQuery(sql);
			query.setMaxResults(1);
			int count = ((Long)query.uniqueResult()).intValue();
			return count > 0 ? true : false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		return false;
	}

	@SuppressWarnings("unchecked")

	public List<RolePo> getAllRole() {
		// TODO Auto-generated method stub
		Session session = null;
		Query query = null;
		try {
			session = this.sessionFactory.getCurrentSession();
			String hql = "From RolePo where isDelete = 0";
			query = session.createQuery(hql);
			return (List<RolePo>)query.list();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		return null;
	}


	public int deleteRoleList(List<RolePo> rolePos) {
		// TODO Auto-generated method stub
		Session session = null;
		try {
			session = this.sessionFactory.getCurrentSession();
			for (int i = 0; i < rolePos.size(); i++) {
				session.update(rolePos.get(i));
			}
			return rolePos.size();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException();
		} 
	}
}
